//定义js命名空间
Ext.namespace('FsCarInfo.FsIntlVehicleAppl');
//DateTime -- date , decimal -- float
// { name: 'Department.Name', mapping: 'Department.Name' },
// { name: 'Department'},
//FsIntlVehicleAppl FieldArray
FsCarInfo.FsIntlVehicleAppl.BaseFieldsArray = new Array(
	{ name: 'Id', type: 'string' },
	{ name: 'ApplUser.Name', mapping: 'ApplUser.Name' },
	{ name: 'ApplUserid', type: 'string' },
	{ name: 'ApplDate', type: 'date' },
	{ name: 'IntlVehicle.CarNo', mapping: 'IntlVehicle.CarNo' },
	{ name: 'CarId', type: 'string' },
	{ name: 'Driver', type: 'string' },
	{ name: 'Business', type: 'string' },
	{ name: 'Type', type: 'string' },
	{ name: 'PlanPath', type: 'string' },
	{ name: 'MileageOut', type: 'int' },
	{ name: 'MileageRet', type: 'int' },
	{ name: 'Mileage', type: 'int' },
	{ name: 'ExpOutTime', type: 'date' },
	{ name: 'ExpRetTime', type: 'date' },
	{ name: 'ActOutTime', type: 'date' },
	{ name: 'ActRetTime', type: 'date' },
	{ name: 'TtCustName', type: 'string' },
	{ name: 'TtCustTel', type: 'string' },
	{ name: 'TtCarNo', type: 'string' },
	{ name: 'TtState', type: 'string' },
	{ name: 'TtReason', type: 'string' },
	{ name: 'TtFee', type: 'float' },
	{ name: 'State', type: 'int' },
	{ name: 'Description', type: 'string' },
	{ name: 'CreateUser', type: 'string' },
	{ name: 'CreateTime', type: 'date' },
	{ name: 'IsDelete', type: 'bool' },
	{ name: 'Version', type: 'int' }
);

//FsIntlVehicleAppl JsonReader
FsCarInfo.FsIntlVehicleAppl.JsonReader = Ext.extend(Infrastructure.BaseJsonReader, {
	constructor: function (cfg) {
		cfg = cfg || {};
		FsCarInfo.FsIntlVehicleAppl.JsonReader.superclass.constructor.call(this, Ext.apply({
			fields: FsCarInfo.FsIntlVehicleAppl.BaseFieldsArray
		}, cfg));
	}
});

//FsIntlVehicleAppl JsonStore
FsCarInfo.FsIntlVehicleAppl.JsonStore = Ext.extend(Infrastructure.BaseJsonStore, {
	constructor: function (cfg) {
		cfg = cfg || {};
		FsCarInfo.FsIntlVehicleAppl.JsonStore.superclass.constructor.call(this, Ext.apply({
			url: '/FsIntlVehicleAppl/GetPlist',
			params: {
				start: 0,
				limit: ALL_PAGESIZE_SETTING
			},
			remoteSort: true,
			fields: FsCarInfo.FsIntlVehicleAppl.BaseFieldsArray,
			listeners: {
				'remove': function (thiz, record, index) {
					Ext.Ajax.request({
						url: '/FsIntlVehicleAppl/Delete?Id=' + record.data.Id,
						success: function (response, opts) {
							var result = Ext.util.JSON.decode(response.responseText);
							if (result.success == false) {
								JsHelper.ShowError(result.msg);
								this.insert(0, record);
							}
						}
					});
				}
			}
		}, cfg));
	}
});

//FsIntlVehicleAppl GridPanel
FsCarInfo.FsIntlVehicleAppl.GridPanel = Ext.extend(Infrastructure.BaseGridPanel, {
	// User property
	baseUrl: '/FsIntlVehicleAppl/',
	formSubmitUrl: '/FsIntlVehicleAppl/SaveOrUpdate', //提交保存Url
	formSubmitMsg: '确定要保存吗?', //提交保存提示信息

	store: new FsCarInfo.FsIntlVehicleAppl.JsonStore(),
	//stateful: true,
	//stateId: 'FsCarInfo.FsIntlVehicleAppl.GridPanel',
	//工具栏标签
	tbarmask: '0',
	//表格列默认显示
	columnMask: '012345600001230000090123000000',
	//状态
	applState: '',

	//constructor
	initComponent: function () {
		//prepare
		var itemGrid = this;
		var tbarMask = this.tbarmask
		var colMask = this.columnMask;
		var cmdAddEntity = this.onAddEntity;
		this.store.setBaseParam('state', this.applState);

		// The filter.
		var gridFilter = new Ext.ux.grid.GridFilters({
			encode: true,
			filters: [
				{ dataIndex: 'ApplUser.Name', type: 'string' },
				{ dataIndex: 'ApplDate', type: 'date' },
				{ dataIndex: 'IntlVehicle.CarNo', type: 'string' },
				{ dataIndex: 'Driver', type: 'string' },
				{ dataIndex: 'Business', type: 'string' },
				{ dataIndex: 'Type', type: 'list', options: JsHelper.GetFilterListData(FsCarInfo.Params.Intl_Vehicle_ApplTypeStore.data.items, 'Code', 'Name') },
				{ dataIndex: 'PlanPath', type: 'string' },
				{ dataIndex: 'MileageOut', type: 'int' },
				{ dataIndex: 'MileageRet', type: 'int' },
				{ dataIndex: 'Mileage', type: 'int' },
				{ dataIndex: 'ExpOutTime', type: 'date' },
				{ dataIndex: 'ExpRetTime', type: 'date' },
				{ dataIndex: 'ActOutTime', type: 'date' },
				{ dataIndex: 'ActRetTime', type: 'date' },
				{ dataIndex: 'TtCustName', type: 'string' },
				{ dataIndex: 'TtCustTel', type: 'string' },
				{ dataIndex: 'TtCarNo', type: 'string' },
				{ dataIndex: 'TtState', type: 'list', options: JsHelper.GetFilterListData(FsCarInfo.Params.Intl_Vehicle_Appl_TTState_Store.data.items, 'Code', 'Name') },
				{ dataIndex: 'TtReason', type: 'string' },
				{ dataIndex: 'TtFee', type: 'int' },
				{ dataIndex: 'State', type: 'list', options: JsHelper.GetFilterListData(FsCarInfo.Params.Intl_Vehicle_Appl_StateStore.data.items, 'Code', 'Name') },
				{ dataIndex: 'CreateTime', type: 'date' },
				{ dataIndex: 'CreateUser', type: 'string' }
			]
		});

		// selModel
		var selModel = new Ext.grid.CheckboxSelectionModel({
			header: '',
			singleSelect: true,
			listeners: {
				'selectionchange': function (sm) {
					// 控制工具栏按钮是否可用
					itemGrid.delBtn.setDisabled(true);
					itemGrid.btnToApprove.setDisabled(true);
					itemGrid.editBtn.setDisabled(true);
					itemGrid.btnApproveAppl.setDisabled(true);
					itemGrid.btnSend.setDisabled(true);
					itemGrid.btnReturn.setDisabled(true);

					// 根据状态控制
					var s = itemGrid.getSelectionModel().getSelections();
					for (var i = 0, r; r = s[i]; i++) {
						switch (r.data.State) {
							case 0:
								itemGrid.delBtn.setDisabled(sm.getCount() < 1);
								itemGrid.editBtn.setDisabled(sm.getCount() != 1);
								itemGrid.btnToApprove.setDisabled(sm.getCount() < 1);
								break;
							case 1:
								itemGrid.btnApproveAppl.setDisabled(sm.getCount() != 1);
								break;
							case 2:
								itemGrid.btnSend.setDisabled(sm.getCount() != 1);
								break;
							case 3:
								itemGrid.btnReturn.setDisabled(sm.getCount() != 1);
								break;
							default: break;
						}
					}
				}
			}
		});

		var approveColumn = new Infrastructure.InfraApproveLog.ApproveColumn({
			//appDisable: this.columnMask.charAt(0) == '0'
			approveUrl: '/FsIntlVehicleAppl/ApproveAppl'
		});

		// columnModel 对于不常需要显示的 hidden: true 
		var gridColumn = new Ext.grid.ColumnModel({
			columns: [
				new Ext.grid.RowNumberer(),
				selModel,
				approveColumn,
				{ header: "申请用户", dataIndex: 'ApplUser.Name' },
				{ header: "申请时间", dataIndex: 'ApplDate', xtype: 'datecolumn', format: 'Y-m-d H:i' },
				{ header: "车辆", dataIndex: 'IntlVehicle.CarNo' },
				{ header: "司机", dataIndex: 'Driver' },
				{ header: "用车类型", dataIndex: 'Type',
					renderer: function (v) {
						return FsCarInfo.Params.Intl_Vehicle_ApplTypeStore.getById(v).get('Name');
					}
				},
				{ header: "用车事由", dataIndex: 'Business', hidden: this.columnMask.charAt(7) == '0' },
				{ header: "计划路线", dataIndex: 'PlanPath', hidden: this.columnMask.charAt(8) == '0' },
				{ header: "出发里程", dataIndex: 'MileageOut', xtype: 'numbercolumn', align: 'right', hidden: this.columnMask.charAt(9) == '0' },
				{ header: "返回里程", dataIndex: 'MileageRet', xtype: 'numbercolumn', align: 'right', hidden: this.columnMask.charAt(10) == '0' },
				{ header: "用车里程", dataIndex: 'Mileage', xtype: 'numbercolumn', align: 'right', hidden: this.columnMask.charAt(11) == '0' },
				{ header: "预计出发", dataIndex: 'ExpOutTime', xtype: 'datecolumn', format: 'H:i', hidden: this.columnMask.charAt(12) == '0' },
				{ header: "预计返回", dataIndex: 'ExpRetTime', xtype: 'datecolumn', format: 'H:i', hidden: this.columnMask.charAt(13) == '0' },
				{ header: "实际出发", dataIndex: 'ActOutTime', hidden: this.columnMask.charAt(14) == '0' },
				{ header: "实际返回", dataIndex: 'ActRetTime', hidden: this.columnMask.charAt(15) == '0' },
				{ header: "客户姓名", dataIndex: 'TtCustName', hidden: this.columnMask.charAt(16) == '0' },
				{ header: "联系电话", dataIndex: 'TtCustTel', hidden: this.columnMask.charAt(17) == '0' },
				{ header: "车牌", dataIndex: 'TtCarNo', hidden: this.columnMask.charAt(18) == '0' },
				{ header: "拖车结果", dataIndex: 'TtState', hidden: this.columnMask.charAt(19) == '0',
					renderer: function (v) {
						return FsCarInfo.Params.Intl_Vehicle_Appl_TTState_Store.getById(v).get('Name');
					}
				},
				{ header: "原因", dataIndex: 'TtReason', hidden: this.columnMask.charAt(20) == '0' },
				{ header: "拖车费用", dataIndex: 'TtFee', xtype: 'numbercolumn', align: 'right', hidden: this.columnMask.charAt(21) == '0' },
				{ header: "状态", dataIndex: 'State', hidden: this.columnMask.charAt(22) == '0',
					renderer: function (v) {
						return FsCarInfo.Params.Intl_Vehicle_Appl_StateStore.getById(v).get('Name');
					}
				},
				{ header: "创建用户", dataIndex: 'CreateUser', hidden: this.columnMask.charAt(23) == '0' },
				{ header: "描述", dataIndex: 'Description', hidden: this.columnMask.charAt(24) == '0' }
			],
			defaults: {
				sortable: true,
				menuDisabled: false
			}
		});

		// PageBar
		var pagebBar = new Infrastructure.BasePagingToolbar({
			store: this.store,
			plugins: this.plugins
		});

		Ext.apply(this, {
			plugins: [gridFilter],
			sm: selModel,
			cm: gridColumn,
			tbar: [{
				xtype: 'tbaraddbtn',
				hidden: tbarMask.charAt(0) != '1',
				handler: cmdAddEntity,
				scope: itemGrid
			}, {
				hidden: tbarMask.charAt(0) != '1',
				xtype: 'tbseparator'
			}, {
				xtype: 'tbareditbtn',
				hidden: tbarMask.charAt(1) != '1',
				handler: function () {
					itemGrid.onLoadEntity(itemGrid.getSelectionModel().getSelected(), true);
				}
			}, {
				hidden: tbarMask.charAt(0) != '1',
				xtype: 'tbseparator'
			}, {
				xtype: 'tbardelbtn',
				hidden: tbarMask.charAt(0) != '1',
				handler: function () {
					itemGrid.delEntity();
				},
				scope: itemGrid
			}, {
				hidden: tbarMask.charAt(0) != '1',
				xtype: 'tbseparator'
			}, {
				xtype: 'tbarButton',
				ref: '../btnToApprove',
				icon: '/Content/icons/key_go.png',
				text: '提交',
				disabled: true,
				hidden: tbarMask.charAt(0) != '1',
				handler: function () {
					itemGrid.tbarSubmit(itemGrid.baseUrl + 'InitApproveLog', '确定要将用车单提交审批流程吗?', '用车单已提交，请等待审批。');
				},
				scope: itemGrid
			}, {
				hidden: tbarMask.charAt(0) != '1',
				xtype: 'tbseparator'
			}, {
				xtype: 'tbarButton',
				ref: '../btnApproveAppl',
				icon: '/Content/icons/key.png',
				text: '审批',
				hidden: true,
				disabled: true,
				handler: function () {
					itemGrid.onLoadEntity(itemGrid.getSelectionModel().getSelected(), true);
				},
				scope: itemGrid
			}, {
				hidden: true,
				xtype: 'tbseparator'
			}, {
				xtype: 'tbarButton',
				ref: '../btnSend',
				icon: '/Content/icons/application_go.png',
				text: '出发',
				disabled: true,
				hidden: tbarMask.charAt(0) != '1',
				handler: function () {
					itemGrid.onLoadEntity(itemGrid.getSelectionModel().getSelected(), true);
				},
				scope: itemGrid
			}, {
				hidden: tbarMask.charAt(0) != '1',
				xtype: 'tbseparator'
			}, {
				xtype: 'tbarButton',
				ref: '../btnReturn',
				icon: '/Content/icons/arrow_undo.png',
				text: '返回',
				disabled: true,
				hidden: tbarMask.charAt(0) != '1',
				handler: function () {
					itemGrid.onLoadEntity(itemGrid.getSelectionModel().getSelected(), true);
				},
				scope: itemGrid
			}, '-', {
				xtype: 'textfield',
				width: 150,
				id: 'q_query'
			}, {
				xtype: 'tbarqrybtn',
				handler: function (e) {
					itemGrid.qryList(itemGrid.q_query.getValue());
				},
				scope: itemGrid
			}],
			bbar: pagebBar,
			listeners: {
				'rowdblclick': function (grid, rowIndex, e) {
					this.onLoadEntity(grid.store.getAt(rowIndex), false);
				}
			}
		});

		FsCarInfo.FsIntlVehicleAppl.GridPanel.superclass.initComponent.call(this);
	},

	/****************** 
	* user function 
	******************/

	// 添加
	onAddEntity: function () {
		var itemGrid = this;
		var itemEditForm = new FsCarInfo.FsIntlVehicleAppl.EditForm({
			isEdit: true,
			listeners: {
				'afterSave': function (ele, rt) {
					if (rt) {
						ele.form.reset();
						var itemGridStore = itemGrid.store;
						itemGridStore.reload();
					}
				}
			}
		});
		var itemEditWin = new FsCarInfo.FsIntlVehicleAppl.EditWin({
			items: [itemEditForm]
		});
		itemEditWin.show();
	},

	// 读取信息显示
	onLoadEntity: function (selRecord, isEdit) {
		var itemGrid = this;
		var selId = selRecord.data.Id;
		var itemEditForm = new FsCarInfo.FsIntlVehicleAppl.EditForm({
			isEdit: isEdit,
			itemId: selId,
			dataRecord: selRecord,
			submitUrl: this.formSubmitUrl,
			submitMsg: this.formSubmitMsg,
			listeners: {
				'afterSave': function (ele, rt) {
					if (rt) {
						itemGrid.store.reload();
					}
				}
			}
		});
		var itemEditWin = new FsCarInfo.FsIntlVehicleAppl.EditWin({
			items: [itemEditForm]
		});
		itemEditWin.show();
	}
});

//FsIntlVehicleAppl EditForm
FsCarInfo.FsIntlVehicleAppl.EditForm = Ext.extend(Infrastructure.BaseForm, {
	isEdit: false, //是否允许编辑
	submitUrl: '/FsIntlVehicleAppl/SaveOrUpdate', //提交保存Url
	submitMsg: '确定要保存吗?', //提交保存提示信息

	initComponent: function () {

		var itemEditForm = this;
		if (this.dataRecord != null)
			this.dataId = this.dataRecord.data.Id;

		// 定义表单列
		var firstColumn = [{
			fieldLabel: "申请人",
			xtype: 'usercombo',
			hiddenName: 'ApplUserid'
		}, {
			fieldLabel: "车辆",
			xtype: 'intl_vehicle_combo',
			hiddenName: 'CarId'
		}, {
			fieldLabel: "类型",
			xtype: 'infra_localdictcombo',
			store: FsCarInfo.Params.Intl_Vehicle_ApplTypeStore,
			hiddenName: 'Type'
		}, {
			fieldLabel: "司机",
			maxLength: 50,
			xtype: 'textfield',
			allowBlank: true,
			name: 'Driver'
		}, {
			fieldLabel: "预计出发",
			xtype: 'datefield',
			format: 'Y-m-d H:i:s',
			value: new Date(),
			hideTrigger: true,
			name: 'ExpOutTime'
		}, {
			fieldLabel: "预计返回",
			xtype: 'datefield',
			format: 'Y-m-d H:i:s',
			value: new Date().add(Date.HOUR, 2),
			hideTrigger: true,
			name: 'ExpRetTime'
		}, {
			fieldLabel: "出发里程",
			xtype: 'numberfield',
			allowBlank: true,
			name: 'MileageOut'
		}, {
			fieldLabel: "返回里程",
			allowBlank: true,
			xtype: 'numberfield',
			name: 'MileageRet'
		}];

		//Form第二列
		var secondColumn = [{
			fieldLabel: "实际出发",
			hideTrigger: true,
			xtype: 'datefield',
			format: 'Y-m-d H:i:s',
			name: 'ActOutTime'
		}, {
			fieldLabel: "实际返回",
			hideTrigger: true,
			xtype: 'datefield',
			format: 'Y-m-d H:i:s',
			name: 'ActRetTime'
		}, {
			fieldLabel: "用车里程",
			xtype: 'numberfield',
			name: 'Mileage'
		}, {
			fieldLabel: "申请时间",
			hideTrigger: true,
			xtype: 'datefield',
			format: 'Y-m-d H:i:s',
			name: 'ApplDate'
		}, {
			fieldLabel: "创建用户",
			xtype: 'textfield',
			name: 'CreateUser'
		}, {
			fieldLabel: "状态",
			allowBlank: true,
			xtype: 'infra_localdictcombo',
			store: FsCarInfo.Params.Intl_Vehicle_Appl_StateStore,
			hiddenName: 'State'
		}];

		//Form单列
		var singleColumn = [
		{
			xtype: 'fieldset',
			title: '拖车信息', // title, header, or checkboxToggle creates fieldset header
			autoHeight: true,
			//columnWidth: 0.5,
			checkboxToggle: true,
			collapsed: false, // fieldset initially collapsed
			//layout: 'anchor',
			labelAlign: 'right',
			labelWidth: 80,
			items: [{
				layout: 'column',
				items: [{
					columnWidth: .5,
					bodyStyle: 'padding:1px',
					layout: 'form',
					defaultType: 'textfield',
					items: [{
						xtype: 'textfield',
						fieldLabel: '客户姓名',
						name: 'TtCustName',
						width: 150
					}, {
						xtype: 'infra_localdictcombo',
						fieldLabel: '拖车结果',
						allowBlank: true,
						hiddenName: 'TtState',
						store: FsCarInfo.Params.Intl_Vehicle_Appl_TTState_Store
					}, {
						xtype: 'textfield',
						fieldLabel: '车牌',
						name: 'TtCarNo',
						width: 150
					}, {
						xtype: 'textfield',
						fieldLabel: '原因',
						name: 'TtReason',
						width: 150
					}]
				}, {
					columnWidth: .5,
					bodyStyle: 'padding:1px',
					layout: 'form',
					defaultType: 'textfield',
					items: [{
						xtype: 'textfield',
						fieldLabel: '联系电话',
						name: 'TtCustTel',
						width: 150
					}, {
						xtype: 'textfield',
						fieldLabel: '费用',
						name: 'TtFee',
						width: 150
					}]
				}]
			}]
		}, {
			xtype: 'textfield',
			fieldLabel: '计划路线',
			name: 'PlanPath',
			allowBlank: false,
			width: 430
		}, {
			xtype: 'textarea',
			fieldLabel: '用车事由',
			name: 'Business',
			allowBlank: false,
			height: 50,
			width: 430
		}, {
			xtype: 'textarea',
			fieldLabel: '说明',
			name: 'Description',
			height: 50,
			width: 430
		}];

		Ext.apply(this, {
			items: [{
				layout: 'column',
				// 注意 现在使用的是两列 要是三列或更多
				// 请改变下面的items
				// 并在上面定义每个列的组件集合
				items: [{
					columnWidth: .5,
					bodyStyle: 'padding:1px',
					layout: 'form',
					defaultType: 'textfield',
					defaults: {
						width: 150,
						allowBlank: false,
						readOnly: !this.isEdit
					},
					items: firstColumn
				}, {
					columnWidth: .5,
					bodyStyle: 'padding:1px',
					layout: 'form',
					defaultType: 'textfield',
					defaults: {
						width: 150,
						readOnly: true
					},
					items: secondColumn
				}]
			}, singleColumn],
			buttons: [{
				xtype: 'btnsubmit',
				hidden: !(this.isEdit && itemEditForm.submitUrl != ''),
				handler: function () {
					itemEditForm.saveItem(itemEditForm.submitUrl)
				},
				scope: itemEditForm
			}, {
				xtype: 'btncancle',
				handler: function () {
					itemEditForm.ownerCt.destroy();
				}
			}],
			reader: new FsCarInfo.FsIntlVehicleAppl.JsonReader()
		});

		FsCarInfo.FsIntlVehicleAppl.EditForm.superclass.initComponent.call(this);
		//定义保存后执行的事件
		this.addEvents('afterSave');

		if (this.dataRecord != null) {
			this.form.loadRecord(this.dataRecord);
		}
	}
});

//FsIntlVehicleAppl DetailWin
FsCarInfo.FsIntlVehicleAppl.EditWin = Ext.extend(Ext.Window, {
	title: '用车申请信息',
	iconCls: 'carb',
	width: 600
});
